#!/usr/pkg/bin/bash /usr/local/bin/bash

#
# Google SVN $Id$
# Final CF SVN Tag Id: make_dir_cfrd.sh 171 2007-02-20 20:33:44Z seklecki
# ====
#

#/*
# * Copyright (c) 2006-2007 Collaborative Fusion, Inc.  All rights reserved.
# * 
# * Developed by: Collaborative Fusion, Inc.
# *                                                      http://www.collaborativefusion.com
# * 
# * Permission is hereby granted, free of charge, to any person obtaining a copy
# * of this software and associated documentation files (the "Software"), to
# * deal with the Software without restriction, including without limitation the
# * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# * sell copies of the Software, and to permit persons to whom the Software is
# * furnished to do so, subject to the following conditions:
# *   1. Redistributions of source code must retain the above copyright notice,
# *      this list of conditions and the following disclaimers.
# *   2. Redistributions in binary form must reproduce the above copyright
# *      notice, this list of conditions and the following disclaimers in the
# *      documentation and/or other materials provided with the distribution.
# *   3. All advertising materials mentioning features or use of this software
# *      must display the following acknowledgement: This product includes 
# *      software developed by Collaborative Fusion, Inc.
# *   4. Neither the name of Collaborative Fusion, Inc. or any of its employees
# *      may be used to endorse or promote products derived from this Software
# *      without specific prior written permission.
#
# * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
# * COLLABORATIVE FUSION, INC. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
# * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
# * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# */
#
#/*
#* This code was written under funding by Collaborative Fusion, Inc.
#*/



if [ ! -f "./cf.conf" ]; then
        echo "Cannot find cf.conf in ./ ... bailing out!";
        exit 1;
else
        source ./cf.conf
fi

# A B-Line for the Door
trap cleanup 1 2 3 14 15

_intStepCount=0;

echo "Building $PLATFORM file system for $FQDN ..."; good2go $?

# Step X: Build the RD Root heiarchy from DSTDIR

if [ -d $CF_ROOT ]; then
        echo "Step ${_intStepCount}: Purging $CF_ROOT"; 
	echo -n "About to purge ${CF_ROOT}/*, confirm: [y/n]:"; 
        read answer
        if [ "$answer" != "y" ]; then
                exit 1;
        fi
        rm -rf ${CF_ROOT}/*; good2go $?
else
        echo "Cannot find & purge $CF_ROOT";
        exit 1;
fi

# Init it as a regex multiple 
USR_EXCLUDE_LIST+="("
# Then append each
for d in $CF_PRUNE_LIST; do USR_EXCLUDE_LIST+="$d|"; done
# A nessecary evil unless we want to make this really complicated
USR_EXCLUDE_LIST+="foobarbiz)";
#echo "Debug: $USR_EXCLUDE_LIST"


# Step X: Build a usr.tgz
echo "Step ${_intStepCount}: Building ${USR_TARBALL} ..."
cd ${DSTDIR}/usr && find . -type f -or -type l | egrep -v $USR_EXCLUDE_LIST  | ${PAX} ${PAX_FLAGS} -w -f ${USR_TARBALL}.tmp.$0.$$ ; good2go $?

echo -n "Disk space of ${USR_TARBALL} before packages: "
du -h ${USR_TARBALL}.tmp.$0.$$

# Step X: Purging old /usr/${PKG_DIRNAME}
echo "Step ${_intStepCount}: Purging old /usr/${PKG_DIRNAME} in usr_root/ and /var/db/pkg in rd_root/"

	echo -n "About to purge: ${USR_ROOT}/* ${RD_ROOT}/var/db/pkg/*, confirm: [y/n]:"; 
	read answer
        if [ "$answer" != "y" ]; then
                exit 1;
        fi
	rm -rf ${USR_ROOT}/* ${RD_ROOT}/var/db/pkg/* ; good2go $?

# Step X: Make a /usr/${PKG_DIRNAME}
echo "Step ${_intStepCount}: Making a /usr/${PKG_DIRNAME} in usr_root/"
mkdir -p ${USR_ROOT}/${PKG_DIRNAME}; good2go $?

# Step X: Installing packages
echo "Step ${_intStepCount}: Installing requested binary packages..."
case $PLATFORM in
	netbsd)
		pkg_add -f -K ${RD_ROOT}/var/db/pkg -p ${USR_ROOT}/${PKG_DIRNAME} ${PKG_LIST} 2>&1 > /dev/null
		echo "DEBUG"
		good2go $?
		;;
	openbsd)
		export PKG_PATH=/usr/ports/packages/i386/all
		export PKG_DBDIR=${RD_ROOT}/var/db/pkg/
		export PKG_DESTDIR=${USR_ROOT}/${PKG_DIRNAME}
		pkg_add -B ${PKG_DESTDIR} -v -f libdepends $PKG_LIST; good2go $?
		;;
esac

echo "Installed package list:"
case $PLATFORM in
	netbsd)
		pkg_info -K ${RD_ROOT}/var/db/pkg
		;;
	openbsd)
		PKG_DBDIR=${RD_ROOT}/var/db/pkg/ pkg_info
		;;
esac


case $PLATFORM in 
	netbsd)
		# Step X: Creating a local MFS /shadow for temp symlink creation
		echo "Step ${_intStepCount}: Creating a local MFS /shadow for temp symlink creation"
		mount_mfs -s 2m swap /shadow
		good2go $?
		
		# Step X: Creating /usr/pkg/etc symlinks to shadow
		echo "Step ${_intStepCount}: Creating /usr/pkg/etc symlinks to /shadow"
		mkdir -p /shadow/usr/pkg/etc
		good2go $?
		
		for a in $PKG_CONFIGS; do
		        echo > /shadow/usr/pkg/etc/${a};
		        cd ${USR_ROOT}/pkg/etc;
		        rm -f ${a};
		        ln -s ../../../shadow/usr/pkg/etc/${a} ./${a};
		done; good2go $?

		# copied form make_dir_mdrd
		for a in ${PKG_CONFIG_DIRS}; do
		        mkdir -p ${RD_ROOT}/shadow/usr/pkg/etc/${a}
		done ; good2go $?

		# this works only if you have PKG_CONFIG_DIRS=fooApp
		# and you need PKG_CONFIGS_2TIER=fooApp/fooConfig.conf
		for b in $PKG_CONFIGS_2TIER; do
		        echo > ${RD_ROOT}/shadow/usr/pkg/etc/${b}; # on MFS
			cd ${USR_ROOT}/pkg/etc;
			rm -f ${b};
			ln -s ../../../../shadow/usr/pkg/etc/${b} ${b};
		done ; good2go $?

		# As an alternative, have PKG_CONFIG_DIRS_LINKED
		# Hybrid mkdir *and* make it a symlink
		# This cant happein Pkg_configs because he'll try
		# to ">" into a file instead of a directory (which is bad?)
		# Of is it...if it's a symlink to a file on MFS
		# but in production the file is replaced with a dir, will it work?
		for c in ${PKG_CONFIG_DIRS_LINKED}; do
			mkdir -p ${RD_ROOT}/shadow/usr/pkg/etc/${c}
			cd ${USR_ROOT}/pkg/etc;
			rm -rf ${c}
			ln -s ../../../shadow/usr/pkg/etc/${c} ${c}
		done; good2go $?

		
		cd ${HOME_DIR}
		
		# Step X: Adjusting any share/examples into rc.d/
		echo "Step ${_intStepCount}:  Placing rc.d scripts from share/examples/rc.d/ into etc/rc.d..."
		for c in ${RCD_EX_SH}; do
			cp ${USR_ROOT}/pkg/share/examples/rc.d/${c} $USR_ROOT/pkg/etc/rc.d;
		done
		good2go $?

		# Step X: Temp nuke bullshit in usr/pkg/share
		echo "Step ${_intStepCount}:  Temp nuking remainder of ${USR_ROOT}/pkg/share/"
		rm -rf ${USR_ROOT}/pkg/share/{vim,samba}
		good2go $?
		;;
esac

# Step X: Append /usr/pkg
echo "Step ${_intStepCount}: Appending contents of usr_root/ package shadow dir into ${USR_TARBALL}"

case $PLATFORM in 
	netbsd)
		cd ${USR_ROOT}
		;;
	openbsd*)
		# Crap
		cd ${USR_ROOT}/local/usr/
		;;
esac

${PAX} ${PAX_FLAGS} -w -a -f ${USR_TARBALL}.tmp.$0.$$ . ; good2go $?
cd ${HOME_DIR}

echo -n "Disk space of ${USR_TARBALL} after packages: "
du -h ${USR_TARBALL}.tmp.$0.$$

# Step X: Compress tarballs if needed
echo "Step ${_intStepCount}: Optionally compressing tarballs."

if checkyesno $COMPRESS_USR_TARBALL ; then
	cat ${USR_TARBALL}.tmp.$0.$$ | gzip -v9 > ${USR_TARBALL}; good2go $?
	rm -f ${USR_TARBALL}.tmp.$0.$$
else
	mv ${USR_TARBALL}.tmp.$0.$$ ${USR_TARBALL}; good2go $?
fi
	
# Step X: Copy the 5 Files to the CF root
echo "Step ${_intStepCount}: Copying contents into a: slice cf_root/"
CF_ROOT_LIST="${USR_TARBALL} \
	${BOOTBLOCK} \
	${BOOTMBR}"

	case $PLATFORM in
		netbsd)
			CF_ROOT_LIST+=" ${BOOT_STAGE_2}"
			;;
	esac

for a in $CF_ROOT_LIST; do
	cp -p "$a" $CF_ROOT;
done
good2go $?

# Step X: Clean up $USR_TARBALL
echo "Step ${_intStepCount}: Cleaning up temp ${USR_TARBALL}"
rm -f ${USR_TARBALL}; good2go $?

case $PLATFORM in 
	netbsd)
		# Step X: Unmount
		echo "Step ${_intStepCount}: Unmounting /shadow"
		umount /shadow; good2go $?
		;;
esac
